package ticketing_system.Database;


import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Database_vipDAO {
    private DBHelper db = new DBHelper(DS.getDataSource());

    public int add(Database_vip vip) throws Exception {
        String sql = "insert into vip(username,password) values(?,?)";
        return db.doUpData(sql, vip.getUsername(), vip.getPassword());
    }

    public int remove(int id) throws Exception {
        String sql = "delete from vip where id=?";
        return db.doUpData(sql, id);
    }

    public int modify(Database_vip vip) throws Exception {
        String sql = "update vip set name=?,pwd=? where id=?";
        return db.doUpData(sql, vip.getUsername(),vip.getPassword());
    }

    public Database_vip queryById(Database_vip vip) throws Exception {
        String sql = "select * from vip where username=? and password=?";
        Handler handler = new Handler() {
            @Override
            public Object handle(ResultSet rs) throws Exception {
                if (rs.next()) {
                    Integer id1 = rs.getInt("id");
                    String name1 = rs.getString("username");
                    String pwd1 = rs.getString("password");
                    Database_vip vip = new Database_vip(id1, name1, pwd1);
                    return vip;
                } else {
                    return null;
                }

            }
        };
        return (Database_vip) db.doQuery(sql,handler,vip.getUsername(),vip.getPassword());
    }
    public List<Database_vip> queryAll() throws Exception {
        String sql = "select * from vip";
        Handler handler = new Handler() {
            @Override
            public Object handle(ResultSet rs) throws Exception {
                List<Database_vip> list = new ArrayList<Database_vip>();
                while (rs.next()) {
                    Integer id1 = rs.getInt("id");
                    String name1 = rs.getString("name");
                    String pwd1 = rs.getString("pwd");
                    Database_vip vip = new Database_vip(id1, name1, pwd1);
                    list.add(vip);
                }
                return list;
            }

        };
        return (List<Database_vip>) db.doQuery(sql, handler);
    }
}
